//
//  EZTallyDictionary.h
//  zhaitu
//
//  Created by ellzu on 13-8-5.
//  Copyright (c) 2013年 ellzu. All rights reserved.
//

#ifndef __zhaitu__EZTallyDictionary__
#define __zhaitu__EZTallyDictionary__

#include <iostream>
#include "EZTallyObject.h"
#include <map>
#include "EZTallyArray.h"

namespace EZTally {
    
    template <class _key,class _value>
    class EZTallyDictionary : public EZTallyObject
    {
    private:
        std::map<_key, _value> mapObjects;
        
    protected:
        virtual void CopyOther(const EZTallyDictionary &other);
    public:
        EZTallyDictionary();
        EZTallyDictionary(const EZTallyDictionary &other);
        EZTallyDictionary &operator=(const EZTallyDictionary &other);
        ~EZTallyDictionary();
        
        void SetObjectForKey(_key key,_value value);
        _value ObjectForKey(_key key);
        void RemoveObjectForKey(_key key);
        bool isHasValue(_key key);
        int Count();
        
        std::vector<_key> AllKeys();

    };
    
    
    
    
    
}



/////////////////
////////cpp/////////
/////////////////
using namespace EZTally;

template<class _key, class _value>
EZTallyDictionary<_key,_value>::EZTallyDictionary()
{
    
}

template<class _key, class _value>
EZTallyDictionary<_key,_value>::EZTallyDictionary(const EZTallyDictionary &other)
{
    CopyOther(other);
}
template<class _key, class _value>
EZTallyDictionary<_key,_value>& EZTallyDictionary<_key,_value>::operator=(const EZTallyDictionary &other)
{
    CopyOther(other);
    return *this;
}

template<class _key, class _value>
EZTallyDictionary<_key,_value>::~EZTallyDictionary()
{
    typename std::map<_key,_value>::iterator it=mapObjects.begin();
    for (; it!=mapObjects.end();it++) {
        EZTallyObject *obj = (EZTallyObject*)it->second;
        obj->Release();
    }
}

template<class _key, class _value>
void EZTallyDictionary<_key,_value>::SetObjectForKey(_key key, _value value)
{
    EZTallyObject *obj = static_cast<EZTallyObject*>(value);
    if (obj==0) {
        return;
    }
    obj->Retain();
    mapObjects[key] = value;
}

template<class _key, class _value>
_value EZTallyDictionary<_key,_value>::ObjectForKey(_key key)
{
    if (mapObjects.find(key)==mapObjects.end()) {
        return 0;
    }
    _value value = mapObjects[key];
    return value;
}

template<class _key, class _value>
bool EZTallyDictionary<_key,_value>::isHasValue(_key key)
{
    return mapObjects.find(key)!=mapObjects.end();
}

template<class _key, class _value>
int EZTallyDictionary<_key,_value>::Count()
{
    return mapObjects.size();
}

template<class _key, class _value>
std::vector<_key> EZTallyDictionary<_key,_value>::AllKeys()
{
    std::vector<_key> keys;
    typename std::map<_key,_value>::iterator it=mapObjects.begin();
    for (; it!=mapObjects.end();it++) {
        _key key = it->first;
        keys.push_back(key);
    }
    
    return keys;
}

template<class _key, class _value>
void EZTallyDictionary<_key, _value>::RemoveObjectForKey(_key key)
{
    typename std::map<_key, _value>::iterator it = mapObjects.find(key);
    if(it != mapObjects.end() ){
        EZTallyObject *obj = static_cast<EZTallyObject*>(it->second);
        obj->Release();
        mapObjects.erase(it);
    }
    
}

template <class _key, class _value>
void EZTallyDictionary<_key, _value>::CopyOther(const EZTallyDictionary &other)
{
    mapObjects = other.mapObjects;
    typename std::map<_key,_value>::iterator it = mapObjects.begin();
    for (; it!=mapObjects.end(); it++) {
        EZTallyObject *o = it->second;
        o->Retain();
    }
}

#endif /* defined(__zhaitu__EZTallyDictionary__) */
